import { getUserInfoService } from '@/apis/login'
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { useCategoryStore } from './category'
import { useCartStore } from './cart'

export const useUserStore = defineStore(
  'rabbit-user',
  () => {
    const categoryStore = useCategoryStore()
    const cartStore = useCartStore()
    const userInfo = ref({})

    const getUserInfo = async (data) => {
      const res = await getUserInfoService(data)
      userInfo.value = res.data.result
      cartStore.getCartList()
      cartStore.mergeCart()
    }
    const clearUserInfo = () => {
      userInfo.value = {}
      categoryStore.categoryList = []
      cartStore.cartList = []
    }

    return {
      userInfo,
      getUserInfo,
      clearUserInfo
    }
  },
  {
    persist: true
  }
)
